
using System;
using System.Collections.Generic;
using System.Web;

namespace OF.Security.Utils
{
	public static class ContextCacheManager
	{
		[ThreadStatic]
		private static Dictionary<Type, ContextCacheQueueBase> cacheDictionary;

		public static T GetInstance<T>() where T : ContextCacheQueueBase
		{
			Type typeFromHandle = typeof(T);
			Dictionary<Type, ContextCacheQueueBase> dictionary = ContextCacheManager.GetCacheDictionary();
			ContextCacheQueueBase contextCacheQueueBase;
			if (!dictionary.TryGetValue(typeFromHandle, out contextCacheQueueBase))
			{
				contextCacheQueueBase = (ContextCacheQueueBase)Activator.CreateInstance(typeof(T), true);
				dictionary.Add(typeFromHandle, contextCacheQueueBase);
			}
			return (T)((object)contextCacheQueueBase);
		}

		private static Dictionary<Type, ContextCacheQueueBase> GetCacheDictionary()
		{
			Dictionary<Type, ContextCacheQueueBase> dictionary;
			if (EnvironmentHelper.Mode == InstanceMode.Web)
			{
				dictionary = (Dictionary<Type, ContextCacheQueueBase>)HttpContext.Current.Items["ContextCacheDictionary"];
			}
			else
			{
				dictionary = ContextCacheManager.cacheDictionary;
			}
			if (dictionary == null)
			{
				dictionary = new Dictionary<Type, ContextCacheQueueBase>();
			}
			if (EnvironmentHelper.Mode == InstanceMode.Web)
			{
				HttpContext.Current.Items["ContextCacheDictionary"] = dictionary;
			}
			else
			{
				ContextCacheManager.cacheDictionary = dictionary;
			}
			return dictionary;
		}
	}
}
